package meijuli.com.join51.business.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;

import com.jhx.common.utils.Constants;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.activity.MainActivity;

import com.jhx.common.cache.SimpleCache;
import com.jhx.common.http.ApiManager;
import com.jhx.common.http.HttpRequest;
import com.jhx.common.http.HttpResponse;
import com.jhx.common.http.ResponseCallback;
import com.jhx.common.model.Event;
import meijuli.com.join51.business.model.Status;
import com.jhx.common.model.User;
import meijuli.com.join51.business.service.ApiService;
import meijuli.com.join51.business.utils.StringUtil;
import com.jhx.common.utils.UserManager;
import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;

/**
 * 认证审核中
 *
 * @author wenqin 2017-04-14 11:41
 */

public class ReviewingFragment extends BaseFragment {
    private Call<HttpResponse<Status>> mCall;
    SwipeRefreshLayout mRefreshLayout;
    TextView mAccount;
    TextView mPhone;
    CircleImageView mHeadIcon;
    TextView mReviewStatusText;
    TextView mTxtReviewing;
    RelativeLayout mHeadView;

    public static ReviewingFragment newInstance(User user) {

        Bundle args = new Bundle();
        args.putSerializable(MainActivity.KEY_USER, user);
        ReviewingFragment fragment = new ReviewingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_reviewing, container, false);
        mRefreshLayout = view.findViewById(R.id.refresh_layout);
        mAccount = view.findViewById(R.id.account);
        mPhone = view.findViewById(R.id.phone);
        mHeadIcon = view.findViewById(R.id.head_icon);
        mReviewStatusText = view.findViewById(R.id.review_status_text);
        mTxtReviewing = view.findViewById(R.id.review_txt);
        mHeadView = view.findViewById(R.id.rl_head);
        return view;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mHeadView.setVisibility(View.VISIBLE);
        if (getArguments() != null) {
            User user = (User) getArguments().getSerializable(MainActivity.KEY_USER);
            if (user == null) {
                user = UserManager.get().getUser();
            }
            if (user == null) {
                return;
            }
            mPhone.setText(StringUtil.hiePhoneNum(user.getPhone()));
            Glide.with(this)
                    .load(user.getHeadImage())
                    .error(R.mipmap.icon_header)
                    .into(mHeadIcon);
            mAccount.setText(StringUtil.limitAccounNameLength(user.getBusinessName()));
            if (user.getAuthStatus() == Constants.ApprovalStatus.APPROVALING) {
                mTxtReviewing.setVisibility(View.GONE);
                mReviewStatusText.setText("认证审核中！");
            }
        }

        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(!mRefreshLayout.isRefreshing()) {
                    mRefreshLayout.setRefreshing(true);
                    getAuthStatus(UserManager.get().getUser());
                }
            }
        });
    }

    private void getAuthStatus(final User user) {
        mCall = ApiManager.get().getApiService(ApiService.class).getAuthStatus();
        HttpRequest.getRequest().autoRecycle(this).start(mCall, new ResponseCallback<Status>() {

            @Override
            public void onSuccess(String url, Status data) {
                mRefreshLayout.setRefreshing(false);
                if (data == null) {
                    return;
                }
                user.setType(data.getType());
                user.setDmId(data.getDmId());
                user.setLevelDesc(data.getLevelDesc());
                user.setEndTime(data.getEndTime());
                user.setAuthStatus(data.getAuthStatus());
                UserManager.get().setUser(user);
                SimpleCache.Companion.put(Constants.IS_SHOW_EXPIRED, true);
                Event event = new Event();
                event.setData(data);
                EventBus.getDefault().post(event);
                pop();
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                super.onError(url, errorCode, errorMsg);
                mRefreshLayout.setRefreshing(false);
            }
        });
    }

}
